package Action;

import Entities.DoeBookEntity;
import Entities.ShoppingTrolleyEntity;
import Service.ShpTrolleyService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.net.httpserver.Authenticator;
import org.apache.struts2.ServletActionContext;

import java.util.List;
import java.util.Map;

//处理购物车模块的Action
public class ShpTrolleyAction extends ActionSupport
{
    private String user_name;
    private String bookId;
    private ShpTrolleyService shpTrolleyService;
    private String addResult;

    private List<ShoppingTrolleyEntity> shpTrolleyList;  //购物车列表
    private int initLimit = 14;
    private String after_id = "0";
    private String limit=String.valueOf(initLimit);
    private List<ShoppingTrolleyEntity> feedList;

    private List<DoeBookEntity> shoppingList;

    public List<DoeBookEntity> getShoppingList() {
        return shoppingList;
    }

    public void setShoppingList(List<DoeBookEntity> shoppingList) {
        this.shoppingList = shoppingList;
    }

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public String getBookId() {
        return bookId;
    }

    public void setBookId(String bookId) {
        this.bookId = bookId;
    }

    public ShpTrolleyService getShpTrolleyService() {
        return shpTrolleyService;
    }

    public void setShpTrolleyService(ShpTrolleyService shpTrolleyService) {
        this.shpTrolleyService = shpTrolleyService;
    }

    public String getAddResult() {
        return addResult;
    }

    public void setAddResult(String addResult) {
        this.addResult = addResult;
    }

    public List<ShoppingTrolleyEntity> getShpTrolleyList() {
        return shpTrolleyList;
    }

    public void setShpTrolleyList(List<ShoppingTrolleyEntity> shpTrolleyList) {
        this.shpTrolleyList = shpTrolleyList;
    }

    public int getInitLimit() {
        return initLimit;
    }

    public void setInitLimit(int initLimit) {
        this.initLimit = initLimit;
    }

    public String getAfter_id() {
        return after_id;
    }

    public void setAfter_id(String after_id) {
        this.after_id = after_id;
    }

    public String getLimit() {
        return limit;
    }

    public void setLimit(String limit) {
        this.limit = limit;
    }

    public List<ShoppingTrolleyEntity> getFeedList() {
        return feedList;
    }

    public void setFeedList(List<ShoppingTrolleyEntity> feedList) {
        this.feedList = feedList;
    }

    //把某本图书添加到购物车
    public String addToShpTrollry()
    {
        String result = shpTrolleyService.addRecord(user_name,bookId);
        if(result.equals("success"))
        {
            setAddResult("成功添加至购物车！");
            return SUCCESS;
        }
        else if(result.equals("exist already"))
        {
            setAddResult("已存在于购物车中！");
            return "exist";
        }
        else
        {
            return "failure";
        }
    }

    //返回某个用户的推荐列表
    public String returnShpTrolleyList()
    {
        String param_after_id = ServletActionContext.getRequest().getParameter("after_id");
        String param_limit = ServletActionContext.getRequest().getParameter("limit");
        setAfter_id(param_after_id != null ? param_after_id : "0");
        setLimit(param_limit != null ? param_limit : String.valueOf(initLimit));
        Map<String, Object> session =  ActionContext.getContext().getSession();
        String user_name = (String) session.get("user_name");  //获取登录或注册时存入session的参数——用户名

        String session_Id = ServletActionContext.getRequest().getSession().getId();

        String result;

        shoppingList = shpTrolleyService.returnShpTrolleyList(user_name);
        return "success";
    }

}